package com.itheima.dao;

import com.itheima.pojo.Menu;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * @author Tony
 * @create 2021-12-07 20:00
 */
public interface MenuDao1 {
    /**
     * 查询所有菜单信息
     *
     * @return the list
     */
    @Select("select * from t_menu")
    List<Menu> findAllMenu();


    /**
     * 添加菜单
     *
     * @param roleId the menu
     * @param menuId the menu
     */
    @Insert("insert into t_role_menu values(#{roleId},#{menuId})")
    void setRoleAndMenu(@Param("roleId") Integer roleId, @Param("menuId") Integer menuId);

    /**
     * 根据角色id查询已选择菜单id
     * @param roleId
     * @return
     */
    @Select("select m.id from t_role_menu rm,t_menu m where rm.role_id=#{roleId} and m.id=rm.menu_id")
    Integer[] findMenuByRoleId(Integer roleId);

    /**
     * 根据角色id删除菜单中间表
     * @param roleId
     */
    @Delete("delete from t_role_menu where t_role_menu.role_id=#{roleId}")
    void deleteRoleAndMenuById(Integer roleId);


    /**
     * 根据角色id删除菜单
     *
     * @param roleId the role id
     */
    @Delete("delete from t_role_menu where t_role_menu.role_id=#{roleId}")
    void deleteMenuById(Integer roleId);

    /**
     * 根据id添加菜单
     * @param menuId1
     * @param roleId
     */
    @Insert("insert into t_role_menu values(#{roleId},#{menuId1})")
    void addMenuById(@Param("menuId1") Integer menuId1, @Param("roleId") Integer roleId);

    /**
     * 根据父id查询父菜单
     *
     * @param id the parent id
     * @return the integer
     */
    @Select("select m.parentMenuId from t_menu m where id=#{id}")
    Integer findParentIdByMenuId(Integer id);

    /**
     * 如果是子菜单先添加父菜单
     *
     * @param roleId the role id
     * @param menuId the menu id
     */
    @Insert("insert into t_role_menu values(#{roleId},#{menuId})")
    void addMenu(@Param("roleId") Integer roleId, @Param("menuId") Integer menuId);

    /**
     * 判断是否存在
     * @param id
     * @return
     */
    @Select("select rm.menu_id from t_role_menu rm where rm.menu_id=#{id} and rm.role_id=#{roleId}")
    Integer isExit(@Param("id") Integer id, @Param("roleId") Integer roleId);
}
